//
//  HHMonthMoneyViewController.m
//  HHBroker
//
//  Created by He Wei on 10/19/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHMonthMoneyViewController.h"
#import "HHDetailMoneyViewController.h"
#import "HHMyMoneyCell.h"
#import "HHMoneyBaseModel.h"
#import "HHMoneySuccessCell.h"
#import "HHMoneyFailCell.h"


@interface HHMonthMoneyViewController ()

@end

@implementation HHMonthMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的佣金";
    [self prepareData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Delegate
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseListMoneyCell = @"reuseListMonehyCell";
    static NSString *reuseSuccessCell = @"reuseSuccessCell";
    static NSString *reuseFailCell = @"reuseFailCell";
    if (indexPath.section == 0) {
        HHMyMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseListMoneyCell];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HHMyMoneyCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        return cell;
    }
    HHMoneyBaseModel *model = self.dataArray[indexPath.row];
    if (model.type != JYContractTypeFail) {
        HHMoneySuccessCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseSuccessCell];
        if (cell == nil) {
            cell =[[[NSBundle mainBundle] loadNibNamed:@"HHMoneySuccessCell" owner:self options:nil] lastObject];
        }
        cell.model = model;
        return cell;
    } else {
        HHMoneyFailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseFailCell];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HHMoneyFailCell" owner:self options:nil] lastObject];
        }
        cell.model = model;
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
    
    HHMoneyBaseModel *model = self.dataArray[indexPath.row];
    if (![model isKindOfClass:[HHMoneyBaseModel class]]) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
    self.index = indexPath;
    HHDetailMoneyViewController *detail = [[HHDetailMoneyViewController alloc] init];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return [HHMyMoneyCell heightWithModel:self.model];
    } else if (indexPath.section == 1) {
        HHMoneyBaseModel *model = self.dataArray[indexPath.row];
        if (model.type != JYContractTypeFail) {
            return [HHMoneySuccessCell heightWithModel:nil];
        } else {
            return [HHMoneyFailCell heightWithModel:nil];
        }
    }
    return 0;
}


#pragma mark - Setter && Getter

- (void)prepareData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *urlString = [NSString stringWithFormat:@"%@&month=%@", KUrlMonthListMoney, self.month];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        [hub hide:YES];
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            id data = [response objectForKey:@"list"];
            if ([data isKindOfClass:[NSArray class]]) {
                NSArray *list = (NSArray *)data;
                for (NSDictionary *dic in list) {
                    HHMoneyBaseModel *model = [[HHMoneyBaseModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
            }
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题呢!";
        [hub hide:YES afterDelay:2];
    } cache:NO];
  

}
@end
